package com.pdstudio.http;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pdstudio.bean.ResponseData;
import com.pdstudio.bean.Warning;
import com.pdstudio.bean.WeatherActual;
import com.pdstudio.bean.WeatherDay;
import com.pdstudio.bean.WeatherDetail;
import com.pdstudio.bean.WeatherFine;
import com.pdstudio.bean.WeatherImg;
import com.pdstudio.bean.WeatherProfile;
import com.pdstudio.bean.WeatherStamp;
import com.pdstudio.bean.WeatherSun;

import android.content.Context;

public class WeatherHttpMethod extends HttpMethodBase {

	private String funcName = "weather";
	private String GETWEATHERFORECASTS = "getweatherforecasts";
	private String GETWEATHERFINE = "getweatherfine";
	private String GETCLOUNDS = "getclouds";
	private String GETREDAS="getradars";
	private String GETWEATHERDETAIL = "getweatherdetail";
	private String GETVERSIONCODE = "getversioncode";
	private String GETWEATHERACTUAL = "getweatheractual";
	private String GETWARNINGS="getwarning";
	private String GETSUN="getsun";
	private String GETWEATHERLIST="getweatherlist";
	private String GETAUDIOPATH="getaudiopath";
	private Context context;

	public WeatherHttpMethod(Context context) {
		super(context);
	}

	public WeatherFine GetWeatherFine(String appKey, String addressCode,
			String type) {
		WeatherFine result = null;
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("address", addressCode);
			parameters.put("type", type);
			String uri = buildUri(funcName, GETWEATHERFINE, appKey, parameters);
			System.out.println(uri);
			ResponseData<WeatherFine> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<WeatherFine>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/*
	 * 获取天气预报信息
	 */
	public WeatherProfile Getweatherforecasts(String appKey, String addressCode) {
		WeatherProfile result = null;
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("address", addressCode);
			String uri = buildUri(funcName, GETWEATHERFORECASTS, appKey,
					parameters);
			System.out.println(uri);
			ResponseData<WeatherProfile> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<WeatherProfile>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 获取云图列表
	 * */
	public List<WeatherImg> GetCloundImgs(String appKey, String addressCode) {
		List<WeatherImg> result = new ArrayList<WeatherImg>();
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("address", addressCode);
			String uri = buildUri(funcName, GETCLOUNDS, appKey, parameters);
			System.out.println(uri);
			ResponseData<List<WeatherImg>> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<List<WeatherImg>>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 获取天气列表
	 * */
	public List<WeatherDay> GetWeatherDays(String appKey, String addressCode) {
		List<WeatherDay> result = new ArrayList<WeatherDay>();
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("address", addressCode);
			String uri = buildUri(funcName, GETWEATHERLIST, appKey, parameters);
			System.out.println(uri);
			ResponseData<List<WeatherDay>> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<List<WeatherDay>>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/*
	 * 获取雷达图*/
	public List<WeatherImg> GetRedaImgs(String appKey, String addressCode) {
		List<WeatherImg> result = new ArrayList<WeatherImg>();
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("address", addressCode);
			String uri = buildUri(funcName, GETREDAS, appKey, parameters);
			System.out.println(uri);
			ResponseData<List<WeatherImg>> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<List<WeatherImg>>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * 获取气象详情
	 */
	public List<WeatherDetail> GetWeatherDetails(String appKey,
			String timestamp, String address) {
		List<WeatherDetail> result = new ArrayList<WeatherDetail>();
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("address", address);
			String uri = buildUri(funcName, GETWEATHERDETAIL, appKey,
					parameters);
			System.out.println(uri);
			ResponseData<List<WeatherDetail>> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<List<WeatherDetail>>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * 获取版本号，是数字
	 */
	public String GetVersionCode(String appKey) {
		String result = "";
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			String uri = buildUri(funcName, GETVERSIONCODE, appKey, parameters);
			System.out.println(uri);
			ResponseData<String> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<String>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * 获取实时天气
	 */
	public WeatherActual GetWeatherActual(String appKey, String address) {
		WeatherActual result = null;
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("address", address);
			String uri = buildUri(funcName, GETWEATHERACTUAL, appKey,
					parameters);
			System.out.println(uri);
			ResponseData<WeatherActual> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<WeatherActual>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/*
	 * 获取预警信息
	 */
	public List<Warning> GetWarning(String appKey, String address) {
		List<Warning> result = new ArrayList<Warning>();
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("address", address);
			String uri = buildUri(funcName, GETWARNINGS, appKey,
					parameters);
			System.out.println(uri);
			ResponseData<List<Warning>> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<List<Warning>>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	/*
	 * 获取日出日落信息
	 */
	public WeatherSun GetSun(String appKey, String address) {
		WeatherSun result = null;
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("address", address);
			String uri = buildUri(funcName, GETSUN, appKey,
					parameters);
			System.out.println(uri);
			ResponseData<WeatherSun> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<WeatherSun>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * 获取指定城市音频播报的地址*/
	public String GetAudioPath(String appKey, String addressCode) {
		String result = "";
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("address", addressCode);
			String uri = buildUri(funcName, GETAUDIOPATH, appKey, parameters);
			System.out.println(uri);
			ResponseData<String> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<String>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
}
